package com.space.school.controller;

import com.space.school.domain.ProductInfo;
import com.space.school.domain.User;
import com.space.school.service.ProductInfoService;
import com.space.school.service.impl.ProductInfoServiceImpl;
import com.space.school.utils.PageBean;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@WebServlet(name = "productInfoByPageServlet", value = "/productInfoByPageServlet")
public class ProductInfoByPageServlet extends HttpServlet {
    ProductInfoService productInfoService = new ProductInfoServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        每页显示页数
        int pageSize=3;
        String product_name = req.getParameter("product_name");
        String product_status = req.getParameter("product_status");
        String currentPage = req.getParameter("currentPage");
        if (currentPage == null || currentPage.equals("")){
            currentPage="1";
        }
        Map<String,String[]> map = req.getParameterMap();
        PageBean<ProductInfo> productInfos = productInfoService.search(pageSize,Integer.parseInt(currentPage),map);
        req.setAttribute("pb", productInfos);
        req.setAttribute("product_name", product_name);
        if(product_status != null&&!product_status.equals("")){
            req.setAttribute("product_status",product_status);
        }
        req.getRequestDispatcher("productInfo.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
//        String username = req.getParameter("userName");
        int pageSize=3;
        String currentPage = req.getParameter("currentPage");
        if (currentPage == null || currentPage.equals("")){
            currentPage="1";
        }
        String product_name = req.getParameter("product_name");
        String product_status = req.getParameter("product_status");
        Map<String,String[]> map = req.getParameterMap();
        PageBean<ProductInfo> productInfos = productInfoService.search(pageSize,Integer.parseInt(currentPage),map);
        req.setAttribute("pb", productInfos);
        req.setAttribute("product_name",product_name);
        if(!product_status.equals("")&&product_status != null){
            req.setAttribute("product_status",product_status);
        }
        req.getRequestDispatcher("productInfo.jsp").forward(req, resp);
    }
}
